package test.ych.com.test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*


const val TAG = "linhaojian"
class MainActivity : AppCompatActivity() {
    var kontlinTest : KotlinTest? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        init()
        addlucencyFragment()
    }

   private fun init(){
       kontlinTest = KotlinTest();
       btn.text = "Hello Kotlin"
       btn.setOnClickListener(View.OnClickListener {
           kontlinTest!!.delegate()// !! 能在函数的参数中调用函数
           Toast.makeText(this,"hello kotlin",Toast.LENGTH_LONG).show() })
//       btn.setOnClickListener { view: View? ->  }
   }

   private fun testUIUnit(){
       btn.text = "UIUnit";
       btn.setOnClickListener { sayUint(); }
   }

   private fun sayUint(){
        tv.text = "Hello,"+et.text.toString()+" ! "
   }

    //添加透明的Fragment检测Activity生命周期
    private fun addlucencyFragment(){
        var fragment = supportFragmentManager.findFragmentByTag("tag")
        if(fragment==null){
            fragment = MyF();
            supportFragmentManager.beginTransaction().add(fragment,"tag").commitAllowingStateLoss()
        }
    }

    class MyF : Fragment(){
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            Log.e(TAG,"onCreate")
        }

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            Log.e(TAG,"onCreateView")
            return super.onCreateView(inflater, container, savedInstanceState)
        }

        override fun onPause() {
            super.onPause()
            Log.e(TAG,"onPause")
        }

        override fun onStop() {
            super.onStop()
            Log.e(TAG,"onStop")
        }

        override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }

        override fun onDestroy() {
            super.onDestroy()
            Log.e(TAG,"onDestroy")
        }
    }

}
